import java.util.*;

public class Test {

    public static void main(String[] args) {
        char[] ch = new char[]{'a', 'b', 'c'};
        String s1 = new String(ch);
        s1.intern();
        String s2 = "abc";
        System.out.println(s1 == s2);
    }

    public static void main2(String[] args) {
        String s1 = "hello";
        String s2 = "hello";

        String s3 = new String("hello");

        System.out.println(s1 == s2); // true

    }
    /*
    public Node copyRandomList(Node head) {
        Map<Node,Node> map = new HashMap<>();
        Node cur = head;
        while(cur != null) {
            Node node = new Node(cur.val);
            map.put(cur,node);

            cur = cur.next;
        }
        //2、第2次遍历链表
        cur = head;
        while(cur != null) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);
    }

     */

    /*
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String str1 = in.nextLine();
            String str2 = in.nextLine();
            func(str1,str2);
        }
    }

    private static void func(String str1,String str2) {
       Set<Character> set = new HashSet<>();
       for(char s : str2.toUpperCase().toCharArray()) {
            set.add(s);
       }

        Set<Character> setBroken = new HashSet<>();
        for(char s : str1.toUpperCase().toCharArray()) {
            if(!set.contains(s) && !setBroken.contains(s)) {
                System.out.print(s);
                setBroken.add(s);
            }
       }
    }
     */

    public static Map<Integer,Integer> countNum(int[] array) {
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < array.length; i++) {
            if(map.get(array[i]) == null) {
                //当前数据 没有出现过
                map.put(array[i],1);
            }else {
                //拆包
                int val = map.get(array[i]);
                map.put(array[i],val+1);
            }
        }
        return map;
    }


    public List<String> topKFrequent(String[] words, int k) {

        //1、统计每个单词出现的次数
        Map<String,Integer> map = new HashMap<>();
        for(String str : words) {
            if(map.get(str) == null) {
                map.put(str,1);
            }else {
                int val = map.get(str);
                map.put(str,val+1);
            }
        }

        //2、遍历map 把前K个元素  放到小根堆当中
        PriorityQueue<Map.Entry<String,Integer>> minHeap = new PriorityQueue<>(
                new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if(o1.getValue().compareTo(o2.getValue()) == 0) {
                    //存储的时候，应该找key变为大根堆存储
                    return o2.getKey().compareTo(o1.getKey());
                }
                return o1.getValue().compareTo(o2.getValue());
            }
        });
        for(Map.Entry<String,Integer> entry : map.entrySet()) {
            if(minHeap.size() < k) {
                //直接存到 小根堆当中
                minHeap.offer(entry);
            }else {
                Map.Entry<String,Integer> top = minHeap.peek();
                if(top.getValue().compareTo(entry.getValue()) < 0) {
                    minHeap.poll();
                    minHeap.offer(entry);
                }else {
                    if(top.getValue().compareTo(entry.getValue()) == 0) {
                        if(top.getKey().compareTo(entry.getKey()) > 0) {
                            //频率一样，字典序小的需要入堆
                            minHeap.poll();
                            minHeap.offer(entry);
                        }
                    }
                }
            }
        }
        List<String> ret = new ArrayList<>();
        for(int i = 0; i < k;i++) {
            Map.Entry<String,Integer> top = minHeap.poll();
            ret.add(top.getKey());//默认是尾插
        }
        Collections.reverse(ret);
        return ret;
    }

    public static void main1(String[] args) {
        int[] array = {1,1,3,4,5,6,3,5,6,7,2,1,3,4};
        Map<Integer,Integer> map = countNum(array);
        //System.out.println(map);
        for(Map.Entry<Integer,Integer> entry :map.entrySet()) {
            System.out.println("数字："+entry.getKey()+" 出现了："+entry.getValue()+" 次！");
        }
    }
}